package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.commen.BaseContext;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.domain.AddressBook;
import com.itheima.reggie.service.IAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;

    //新增地址
    @PostMapping
    public R saveAddress(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("保存地址成功");
    }

    //设置默认地址
    @PutMapping("/default")
    public R defaultAddress(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        qw.set(AddressBook::getIsDefault, 0);
        addressBookService.update(qw);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("设置成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    @GetMapping("default")
    public R getDefault(HttpSession session) {
        Object user = session.getAttribute("user");

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, user).eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(qw);
//        return R.success(one);
        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }

    }

    @GetMapping("/list")
    public R list(HttpSession session) {
        Object user = session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, user);
        List<AddressBook> list = addressBookService.list(qw);
        if (null == list) {
            return R.error("没有找到该对象");
        } else {
            return R.success(list);
        }
    }

    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids) {
        addressBookService.removeByIds(ids);
   /*     LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.in(AddressBook::getId, ids);
        addressBookService.remove(qw);*/
        return R.success("删除成功");
    }

    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }
}
